package com.hjli.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.hjli.tool.FileTool;
import com.hjli.tool.QiNiuTool;


/**
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * 
 * 注释：各种文件上传
 * 时间：2017-07-01
 * 作者：Haojie Li
 * 
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
 * */
@Controller
@RequestMapping("sys/upload")
public class UploadController {

	/**
	 *  上传demo
	 *  success : 0 | 1,           // 0 表示上传失败，1 表示上传成功
	 *  message : "提示的信息，上传成功或上传失败及错误信息等。",
	 *  url     : "图片地址"        // 上传成功时才返回
	 * */
	/*@RequestMapping("demo")  
    public @ResponseBody Map<String,Object> demo(@RequestParam(value = "editormd-image-file", required = false) MultipartFile file, HttpServletRequest request) {  
		Map<String,Object> resultMap = new HashMap<String,Object>();
        String realPath = request.getRealPath("/")+"assets/upload/";
        String fileName = file.getOriginalFilename();  
        File targetFile = new File(realPath, fileName);
        if(!targetFile.exists()){  
            targetFile.mkdirs();  
        }  
        //保存  
        try {  
            file.transferTo(targetFile);  
            resultMap.put("success", 1);
            resultMap.put("message", "上传成功！");
            resultMap.put("url",request.getContextPath()+"/assets/upload/"+fileName);
        } catch (Exception e) {  
        	resultMap.put("success", 0);
            resultMap.put("message", "上传失败！");
            e.printStackTrace();  
        }  
        return resultMap;  
    }*/  
	
	/**
	 *  上传demo
	 *  success : 0 | 1,           // 0 表示上传失败，1 表示上传成功
	 *  message : "提示的信息，上传成功或上传失败及错误信息等。",
	 *  url     : "图片地址"        // 上传成功时才返回
	 * @throws IOException 
	 * */
	@RequestMapping("dropzone")  
    public @ResponseBody Map<String,Object> dropzone(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) throws IOException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = new Date();
		String saveFileName = sdf.format(date);
		
		Map<String,Object> resultMap = new HashMap<String,Object>();
		String realPath = request.getRealPath("/")+"assets/upload/";
		String fileName = file.getOriginalFilename();  
		String suffix = fileName.substring(fileName.lastIndexOf("."));
        String filePath = realPath + saveFileName+suffix;
      
        File targetFile = new File(realPath, saveFileName+suffix);  
        if(!targetFile.exists()){  
            targetFile.mkdirs();  
        }  
        //保存  
        try {  
            file.transferTo(targetFile);  
            //上传七牛云
            new QiNiuTool().upload(filePath,saveFileName+suffix);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return resultMap;  
    }  
	
	
}
